﻿using System;
using System.Collections.Generic;
using personalplaner.business.common;
using personalplaner.business.stamm;
using personalplaner.common;
using personalplaner.dataprovider;

namespace personalplaner.servicemodel.dataaccess
{
	public interface ICommonDataAccess
	{
		#region Get Methods

		/// <summary>
		/// Creates a list containing all months that can be planed in from the active year
		/// </summary>
		/// <returns></returns>
		IEnumerable<PlanMonth> GetActiveMonths();

		/// <summary>
		/// Creates a list containing all months that are planed in a given year
		/// </summary>
		/// <param name="yearid"></param>
		/// <returns></returns>
		IEnumerable<PlanMonth> GetPlanedMonths(int yearid);

		int GetSchemaCount();

		IEnumerable<Feiertag> GetFeiertage(int year);

		/// <summary>
		/// Gets the highest year that a plan was created in
		/// </summary>
		/// <returns></returns>
		int GetMaxYear();

		/// <summary>
		/// Gets a list of years that have been created
		/// </summary>
		/// <returns></returns>
		IEnumerable<PlanYear> GetActiveYears();

        int GetActiveJahrId();

        int GetActiveJahr();

        string GetActiveMandid();

		#endregion

		#region Set Methods

		/// <summary>
		/// Saves hours in a month
		/// </summary>
		/// <param name="atid">atid</param>
		/// <param name="days">days to work in month</param>
		bool SaveMonthDays(int atid, double days);

		/// <summary>
		/// saves a new feiertag and returns the atid
		/// </summary>
		/// <param name="feiertag"></param>
		/// <returns></returns>
		int SaveFeiertag(Feiertag feiertag);

        bool CreateNewYear(int year);

        /// <summary>
        /// sets all person to the new year and initializes the working and holiday times
        /// </summary>
        /// <param name="oldyearid"></param>
        /// <returns></returns>
        bool SetPersonZeiten(int oldyearid);

        /// <summary>
        /// Adds a mandant to the database
        /// </summary>
        /// <param name="mandid">shortcut/primary id for the mandant.</param>
        /// <param name="name">name of the mandant</param>
        /// <param name="description">description of the mandant</param>
        bool SetMandant(string mandid, string name, string description);

		#endregion

		#region Update Methods

		bool UpdateFeiertag(Feiertag feiertag);

		/// <summary>
		/// Reactivates an old year and deactivates all others
		/// </summary>
		/// <param name="year"></param>
		/// <returns></returns>
		bool ReactivateYear(int year);

		#endregion

        #region Del Methods

        bool DelFeiertag(int atid);

        #endregion
    }

	public class CommonDataAccess : BaseDataAccess, ICommonDataAccess
	{
		public CommonDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region Get Methods

		/// <summary>
		/// Creates a list containing all months that can be planed in from the active year
		/// </summary>
		/// <returns></returns>
		public IEnumerable<PlanMonth> GetActiveMonths()
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetActiveMonths(Settings.DatabaseSettings, Settings.Mandant);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
		/// Creates a list containing all months that are planed in a given year
		/// </summary>
		/// <param name="yearid"></param>
		/// <returns></returns>
		public IEnumerable<PlanMonth> GetPlanedMonths(int yearid)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetPlanedMonths(Settings.DatabaseSettings, yearid);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		public int GetSchemaCount()
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetSchemaCount(Settings.DatabaseSettings, Settings.Mandant);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		public IEnumerable<Feiertag> GetFeiertage(int year)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetFeiertage(Settings.DatabaseSettings, year);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

		/// <summary>
		/// Gets the highest year that a plan was created in
		/// </summary>
		/// <returns></returns>
		public int GetMaxYear()
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetMaxYear(Settings.DatabaseSettings);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

		/// <summary>
		/// Gets a list of years that have been created
		/// </summary>
		/// <returns></returns>
		public IEnumerable<PlanYear> GetActiveYears()
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.GetActiveYears(Settings.DatabaseSettings);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return null;
			}
		}

        public int GetActiveJahrId()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.GetActiveJahrId(Settings.DatabaseSettings, Settings.Mandant);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return 0;
            }
        }

        public int GetActiveJahr()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.GetActiveJahr(Settings.DatabaseSettings, Settings.Mandant);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return 0;
            }
        }

        public string GetActiveMandid()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.GetMandid(Settings.DatabaseSettings);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return "";
            }
        }

		#endregion

		#region Set Methods

		/// <summary>
		/// Saves hours in a month
		/// </summary>
		/// <param name="atid">atid</param>
		/// <param name="days">days to work in month</param>
		public bool SaveMonthDays(int atid, double days)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.SaveMonthDays(Settings.DatabaseSettings, atid, days);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		/// <summary>
		/// saves a new feiertag and returns the atid
		/// </summary>
		/// <param name="feiertag"></param>
		/// <returns></returns>
		public int SaveFeiertag(Feiertag feiertag)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.SaveFeiertag(Settings.DatabaseSettings, feiertag);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return 0;
			}
		}

        public bool CreateNewYear(int year)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.CreateNewYear(Settings.DatabaseSettings, Settings.Mandant, year);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        /// <summary>
        /// sets all person to the new year and initializes the working and holiday times
        /// </summary>
        /// <param name="settings"></param>
        /// <param name="oldyearid"></param>
        /// <returns></returns>
        public bool SetPersonZeiten(int oldyearid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.SetPersonZeiten(Settings.DatabaseSettings, oldyearid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        /// <summary>
        /// Adds a mandant to the database
        /// </summary>
        /// <param name="mandid">shortcut/primary id for the mandant.</param>
        /// <param name="name">name of the mandant</param>
        /// <param name="description">description of the mandant</param>
        public bool SetMandant(string mandid, string name, string description)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.SetMandant(Settings.DatabaseSettings, mandid, name, description);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

		#endregion

		#region Update Methods

		public bool UpdateFeiertag(Feiertag feiertag)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.UpdateFeiertag(Settings.DatabaseSettings, feiertag);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		/// <summary>
		/// Reactivates an old year and deactivates all others
		/// </summary>
		/// <param name="year"></param>
		/// <returns></returns>
		public bool ReactivateYear(int year)
		{
			using (new WaitCursor())
			{
				try
				{
					return DataServices.CommonService.ReactivateYear(Settings.DatabaseSettings, year);
				}
				catch (Exception e)
				{
					//TODO: show a messagebox here with the error
					Console.Write(e);
				}

				return false;
			}
		}

		#endregion

        #region Del Methods

        public bool DelFeiertag(int atid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.DelFeiertag(Settings.DatabaseSettings, atid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        #endregion
	}
}
